<?php
namespace app\task\controller;
use think\Request;
use think\Exception;
use think\Db;
use app\common\controller\Common;
class Task extends Common
{
	/**
	 * 任务管理
	 */
	public function index()
	{
		if(Request()->param('')){
			if(Request()->isAjax()){
				$ty = Request()->param('ty');
				return $this->index_model($ty);
			}
			return $this->fetch('');
		}
	}
	/**
	 * 新建自定义任务
	 */
	public function add()
	{
		if(Request()->isGet()){
			return $this->fetch('');
		}
		if(Request()->isPost()){
			$data = Request()->param('');
			try{
				$model = new \app\user\model\Visit;
				$model->admin_id = session('admin.admin_id');
				$model->content = $data['content'];
				$model->state = 2;
				$model->start_time = strtotime($data['start']);
				$model->end_time = strtotime($data['end']);
				$model->save();
				$this->success(config('status.SUCCESS_ONE'));
			}catch(Exception $e){
				$this->error(config('status.ERROR_FIVE'));
			}
		}
	}
	/**
	 * 删除自定义任务
	 */
	public function del()
	{
		if(Request()->isPost()){
			$data = Request()->param('');
			$model = new \app\user\model\Visit;
			try{
				$model->where('visit_log_id',$data['id'])->delete();
				$this->success(config('status.SUCCESS_ONE'));
			}catch(Exception $e){
				$this->error(config('status.ERROR_FIVE'));
			}
		}
	}
	/**
	 * 完成拜访
	 */
	public function finish()
	{
		if(Request()->isPost()){
			$data = Request()->param('');
			$model = new \app\user\model\Visit;
			try{
				$model->where('visit_log_id',$data['id'])->update(['addtime'=>time()]);
				$this->success(config('status.SUCCESS_ONE'));
			}catch(Exception $e){
				$this->error(config('status.ERROR_FIVE'));
			}
		}
	}
	/************************************** 私有 ************************************/
	/**
	 * 任务管理模型
	 */
	private function index_model($ty)
	{
		$model = new \app\user\controller\Visit;
		if($ty == 0){/*客户拜访名单*/
			return $model->get_visit_list();
		}else if($ty == 1){

		}else if($ty == 2){
			return $model->get_index();
		}
	}
}